﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MapTeams.Umbraco.Polls.Services;

namespace MapTeams.Umbraco.Polls.UI.Controls
{
    public partial class ViewPollsList : System.Web.UI.UserControl
    {
        public string AddressForAddEdit { get; set; }
        public string AddressForTestingWidget { get; set; }

        private readonly string DefaultTestPage = PollsConfig.AdminTryPage;


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                divMsg.Visible = false;
                InitSettings();
                if (!IsPostBack)
                {
                    //validate all required properties
                    string errorMessage = IsValid();
                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        divMsg.Visible = true;
                        divMsg.InnerHtml = string.Format("<b>Error validating: </b> {0} ", errorMessage);

                        return;
                    }
                    listPolls();
                }
            }
            catch (Exception ex)
            {
                divMsg.Visible = true;
                divMsg.InnerHtml = ex.Message;
            }

        }

        private void InitSettings()
        {
            if (string.IsNullOrEmpty(AddressForTestingWidget))
                AddressForTestingWidget = DefaultTestPage;
            
        }

        private string IsValid()
        {
            string errorMessage = string.Empty;
            if (string.IsNullOrEmpty(AddressForAddEdit))
                errorMessage += "AddressForAddEdit has not been set";

            return errorMessage;
        }


        private void listPolls()
        {
            PollsServiceLayer objPoll = new PollsServiceLayer(); //todo: use factory design pattern later //inject dependency from webConfig
            DataTable dtPoll = objPoll.ListPoll();

            gvPolls.DataSource = dtPoll;
            gvPolls.DataBind();
        }

        protected void gvPolls_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int pollID = int.Parse(gvPolls.DataKeys[e.RowIndex]["PollID"].ToString());

            PollsServiceLayer objPoll = new PollsServiceLayer();
            objPoll.DeletePoll(pollID);

            divMsg.InnerHtml = "<b>Note:</b> Selected Poll deleted successfully.";
            divMsg.Visible = true;

            listPolls();
        }

        protected string DisplayAddEditUrl(object pollIdPar)
        {
            string pollId = Convert.ToString(pollIdPar);
            string result = string.Format("{0}?{1}={2}", AddressForAddEdit, Constants.PollIdPar, pollId);
            return result;

            //return HttpUtility.HtmlEncode(myFoo.Bar.Baz.ToString());
        }

        protected string DisplayTestWidgetUrl(object pollIdPar)
        {
            string pollId = Convert.ToString(pollIdPar);
            string result = string.Format("{0}?{1}={2}", AddressForTestingWidget, Constants.PollIdPar, pollId);
            return result;

            //return HttpUtility.HtmlEncode(myFoo.Bar.Baz.ToString());
        }

    }
}